<?php

include_once(PS_ADMIN_DIR . '/../classes/AdminTab.php');

class AdminOpinion extends AdminTab {

    public function __construct() {
        $this->table = 'opinion';
        $this->className = 'Opinion';
        $this->name = 'blockopinion';
        $this->lang = false;
        $this->edit = false;
        $this->view = true;
        $this->delete = true;

        $this->fieldsDisplay = array(
            'name' => array('title' => $this->l('Name'), 'align' => 'left', 'width' => 250),
            'rating' => array('title' => $this->l('Rating'), 'width' => 100, 'align' => 'center', 'callback' => 'getRatingIcon'),
            'visible' => array('title' => $this->l('Displayed'), 'active' => 'visible', 'align' => 'center', 'type' => 'bool', 'orderby' => false),
            'created_at' => array('title' => $this->l('Date'), 'width' => 90, 'align' => 'left'));

        parent::__construct();
    }

    public function display() {
        global $cookie;
        if (isset($_GET['view' . $this->table]))
            $this->viewDetails();
        else {
            $this->getList((int) ($cookie->id_lang), !Tools::getValue($this->table . 'Orderby') ? 'created_at' : NULL, !Tools::getValue($this->table . 'Orderway') ? 'DESC' : NULL);
            $this->displayList();
        }
    }

    public function viewDetails() {
        global $currentIndex, $cookie;

        if (!($opinion = $this->loadObject(true)))
            return;
        echo '<fieldset>';
        echo '<div class="opinion-header"><label>Imię lub pseudonim:</label> ';
        echo $opinion->name;
        echo '</div>';
        echo '<div class="opinion-header"><label>Rating:</label> ';
        echo '</div>';
        echo str_repeat('<img src="../img/admin/rating.png" alt="" />', (int) $opinion->rating);
        echo '<div class="opinion-header"><label>Wiadomość:</label>';
        echo $opinion->message;
        echo '</div>';
        echo '<div class="opinion-header"><label>&nbsp;</label>';
        echo '<form action="' . $currentIndex . '&submitShow' . $this->table . '=1&token=' . $this->token . '" method="post">';
        echo '<input type="hidden" name="id_opinion" value="' . $opinion->id_opinion . '" />';
        echo  $opinion->visible == 0 ? '<input class="button" name="accept" type="submit" value="Akceptuj" >' : '<input name="reject" class="button" type="submit" value="Odrzuć" >';
        echo '</form>';
        echo '</div>';
        echo '</fieldset>';
    }

    public function postProcess() {
        global $currentIndex;

        $token = Tools::getValue('token') ? Tools::getValue('token') : $this->token;

        if (Tools::isSubmit('submitShow' . $this->table)) {
            $id_opinion = Tools::getValue('id_opinion');
            if (!($opinion = $this->loadObject(true)))
                return;
            if (Tools::getValue('accept')) {
                $opinion->setVisible(true);
            }
            
            if (Tools::getValue('reject')) {
                $opinion->setVisible(false);
            }

            
        }
    }

}

?>